package com.vstudy.zx.controller;

import com.alibaba.fastjson.JSONObject;
import com.vstudy.zx.entity.*;
import com.vstudy.zx.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/zx/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/publish", method = RequestMethod.GET)
    public String addPage(Model model) {
        List<Grade> grades = articleService.getGrades();
        List<Subject> subjects = articleService.getSubjects();
        model.addAttribute("grades", grades);
        model.addAttribute("subjects", subjects);

        return "zx/article/add.jsp";
    }

    @RequestMapping(value = "/getOutline", method = RequestMethod.POST)
    @ResponseBody
    public Object getOutline(Long grade_id, Long subject_id) {
        List<Outline> outline = articleService.getOutline(grade_id, subject_id);
        return outline;
    }

    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    @ResponseBody
    public Object publish(String articleHtml, Long outline_id, String title, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        boolean rs = articleService.saveArticle(articleHtml, title, teacher.getTe_id(), outline_id, request);
        if (rs) {
            return 1;
        }
        return 0;
    }

    // 文章details
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Long id, Model model, HttpServletRequest request) {
        Article article = articleService.getArticleById(id, request);

        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        if (teacher == null) {
            return "login.jsp";
        }
        // role : 1-teacher
        ArticleZan zan = articleService.getZan(article.getArticle_id(), 1, teacher.getTe_id());
        model.addAttribute("article", article);
        model.addAttribute("zan", zan);

        // star
        StarComments star = articleService.getStarComments(id, 1, teacher.getTe_id());
        model.addAttribute("star", star);

        // 阅读次数加一
        articleService.addReadCount(id);
        return "article/detail.jsp";
    }

    // 根据文章IDs获取文章内容
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getArticles")
    @ResponseBody
    public Object getArticles(Long[] id, @RequestBody JSONObject[] obj, HttpServletRequest request) {
        Set<Long> ids = new HashSet<>();
        for (JSONObject o :
                obj) {
            ids.add(o.getLong("id"));
        }
        List<Article> articles = articleService.getArticles(ids, request);
        return articles;
    }


    @RequestMapping(value = "/zan", method = RequestMethod.POST)
    @ResponseBody
    public Object zan(Long id, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        if (teacher == null) {
            return "login.jsp";
        }
        // role : 1-teacher
        boolean rs = articleService.zan(id, 1, teacher.getTe_id());
        return 1;
    }
}
